
#include "sprite_touch_sample.h"
#include <SPI.h>

#define IWIDTH 160
#define IHEIGHT 128

SpriteTouchSample::SpriteTouchSample() : tft(NULL), img(NULL)
{
    tft = new TFT_eSPI();
    img = new TFT_eSprite(tft);
    colour = red << 11;
}

SpriteTouchSample::~SpriteTouchSample()
{
}

void SpriteTouchSample::setup()
{
    Serial.begin(115200);
    Serial.println("SpriteTouchSample ESP32_SPI_9431_v3");
    tft->init();
    tft->setRotation(0);
    uint16_t calData[5] = {238, 3533, 403, 3507, 4};
    tft->setTouch(calData);
    tft->fillScreen(TFT_BLACK);
    Serial.println("Touch screen to test");
    tft->drawCentreString("Touch screen to test!", tft->width() / 2, tft->height() / 2, 2);

    img->createSprite(IWIDTH, IHEIGHT);
    if (!img->created())
    {
        Serial.println("Sprite not created!");
        return;
    }
    img->fillSprite(TFT_BLACK);

    targetTime = millis() + 1000;
    Serial.println("SpriteTouchSample setup done, targetTime = " + String(targetTime) + ", millis() = " + String(millis()));
}

void SpriteTouchSample::loop()
{
    Serial.println("SpriteTouchSample loop 1");
    String msg1 = "SpriteTouchSample loop 2, targetTime = ";
    String msg2 = String(targetTime);
    Serial.println(msg1 + msg2);
    
    String msg3 = " 3 , millis() = ";
    String msg4 = String(millis());
    Serial.println(msg1 + msg2 + msg3 + msg4);
    Serial.println("SpriteTouchSample loop 4, targetTime = " + String(targetTime) + ", millis() = " + String(millis()));
    if (targetTime < millis())
    {
        targetTime = millis() + 200; // 10000;
        Serial.println("SpriteTouchSample loop 5");
        for (int i = 0; i < 160; i++)
        {
            img->drawFastVLine(i, 0, img->height(), colour);
            switch (state)
            {
            case 0:
                green += 2;
                if (green == 64)
                {
                    green = 63;
                    state = 1;
                }
                break;
            case 1:
                red--;
                if (red == 255)
                {
                    red = 0;
                    state = 2;
                }
                break;
            case 2:
                blue++;
                if (blue == 32)
                {
                    blue = 31;
                    state = 3;
                }
                break;
            case 3:
                green -= 2;
                if (green == 255)
                {
                    green = 0;
                    state = 4;
                }
                break;
            case 4:
                red++;
                if (red == 32)
                {
                    red = 31;
                    state = 5;
                }
                break;
            case 5:
                blue--;
                if (blue == 255)
                {
                    blue = 0;
                    state = 0;
                }
                break;
            }
            colour = red << 11 | green << 5 | blue;
        }
    }
    
    // The standard ADAFruit font still works as before
    img->setTextColor(TFT_BLACK);
    img->setCursor(15, 2);
    // img->drawString("Font size 1", 2, 0, 1); // Draw text at position 2,0 using font 1
    // The new larger fonts do not use the .setCursor call, coords are embedded
    img->setTextColor(TFT_WHITE, TFT_WHITE); // Do not plot the background colour
    // Overlay the black text on top of the rainbow plot (the advantage of not drawing the background colour!)
    img->drawCentreString("Font size 2", 80, 14, 2); // Draw text centre at position 80, 12 using font 2
    // img->drawCentreString("Font size 2",81,12,2); // Draw text centre at position 80, 12 using font 2
    img->drawCentreString("Font size 4", 80, 30, 4); // Draw text centre at position 80, 24 using font 4
    img->drawCentreString("12.34", 80, 54, 6); // Draw text centre at position 80, 24 using font 6

    img->drawCentreString("12.34 is in font size 6", 80, 92, 2); // Draw text centre at position 80, 90 using font 2

    // Note the x position is the top left of the font!    
    // draw a floating point number
    float pi = 3.14159; // Value to print
    int precision = 3;  // Number of digits after decimal point
    int xpos = 50;      // x position
    int ypos = 110;     // y position
    int font = 2;       // font number only 2,4,6,7 valid. Font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 0 : a p m
    xpos += img->drawFloat(pi, precision, xpos, ypos, font); // Draw rounded number and return new xpos delta for next print position
    img->drawString(" is pi", xpos, ypos, font);             // Continue printing from new x position
    img->pushSprite(0, 0);
}